import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {PlanService} from "@train/plan/plan.service";
import {ActivatedRoute, NavigationExtras, Params, Router} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";

@Component({
  selector: 'seed-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.component.scss'],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class EditComponent implements OnInit {
  public params: Object;
  public className: string;

  constructor(private _planService: PlanService,
              private _route: ActivatedRoute,
              private _toolService: ToolsService,
              private _authService: AuthService,
              private _router: Router,
              private cdref: ChangeDetectorRef) {
    this._route.params
      .switchMap((params: Params) => {
        this.initParams();
        this.params['classId'] = params['classId'];
        return this._route.queryParams;
      }).subscribe((params: Params) => {
      this.className = params['className'] || '';
      this.params['id'] = params['id'] ? params['id'] : '';
    });
  }

  initParams() {
    const authOrgId = this._authService.getAuthOrgId();

    this.params = {
      id: '',
      authOrgId: authOrgId,
      classId: '',
      analysis: '',
      target: '',
      measure: '',
    };
  }

  /**
   * @description
   * 保存教学计划
   * @author @chenwh576
   * @date 2018.01.02
   */
  saveTeachScheme() {
    const data = this.params;
    this._planService.saveTeachScheme(data)
      .subscribe(res => {
        if (res.result === '0') {
          const navigationExtras: NavigationExtras = {
            queryParams: {className: this.className}
          };
          this._router.navigate(['/train/plan/class', this.params['classId']], navigationExtras)
          this._toolService.showToast(res.msg);
        }
      });
  }

  /**
   * @description
   * 更新教学计划
   * @author @chenwh576
   * @date 2018.01.02
   */
  updateTeachScheme() {
    const data = this.params;
    this._planService.updateTeachScheme(data)
      .subscribe(res => {
        if (res.result === '0') {
          history.back();
          this._toolService.showToast(res.msg);
        }
      });
  }

  /**
   * @description
   * 根据是否有id判断是执行saveTeachScheme() or updateTeachScheme()
   * @author @chenwh576
   * @date 2018.01.02
   */
  handleTeachScheme() {
    if (this.params['id'] === '') {
      this.saveTeachScheme();
    } else {
      this.updateTeachScheme();
    }
  }

  /**
   * @description
   * 根据班级ID获取 班级课时列表
   * @author @chenwh576
   * @time 2018.01.02
   * @param {number} classId
   * @return {Observable<SNSResult<TrainClassroomScheme>>}
   */
  fetchTeachScheme(): void {
    this._planService.fetchTeachScheme(this.params['classId'])
      .subscribe(res => {
        // this.teachScheme = res.data;
        /* this.params['analysis'] = res.data.analysis;
         this.params['target'] = res.data.target;
         this.params['measure'] = res.data.measure;*/
        const {analysis, target, measure} = res.data,
        params: any = this.params;
        params.analysis = analysis;
        params.target = target;
        params.measure = measure;
        this.cdref.detectChanges();
      });
  }

  handleTeachSchemeValue(event, type: string) {
    switch (type) {
      case 'analysis':
        this.params['analysis'] = event;
        break;
      case 'target':
        this.params['target'] = event;
        break;
      case 'measure':
        this.params['measure'] = event;
        break;
    }

  }

  get isDisabled(): boolean {
    if (!!this.params['analysis']
      || !!this.params['target']
      || !!this.params['measure']
    ) {
      return false;
    } else {
      return true;
    }
  }

  ngOnInit() {
    this.fetchTeachScheme();
  }

}
